define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'customer/service/index' + location.search,
                    // add_url: 'customer/service/add',
                    edit_url: 'customer/service/edit',
                    // del_url: '
                    // customer/service/del',
                    multi_url: 'customer/service/multi',
                    import_url: 'customer/service/import',
                    table: 'customer_service',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                queryParams: (params) => {
                    var filter = JSON.parse(params.filter)
                    var op = JSON.parse(params.op)
                    filter.month = 0
                    op.month = '<>'
                    filter['package.package_id'] = Config['package_ids']
                    op['package.package_id'] = 'in'
                    params.filter = JSON.stringify(filter)
                    params.op = JSON.stringify(op)
                    return params
                },
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id', title: __('Id'),
                            operate: false,
                        },
                        {field: 'package.admin_info.nickname', title: __('销售')},
                        {field: 'package.kuaiji_names', title: __('会计')},
                        {field: 'package.waiqin_names', title: __('外勤')},
                        {
                            field: 'status',
                            title: __('到期状态'),
                            operate: false,
                            // formatter: Table.api.formatter.status,
                            formatter: function (value, row, index) {
                                if (row.distance_days == '-') {
                                    return row.distance_days
                                } else if (row.distance_days <= 2) {
                                    return '<span class="text-orange"><i class="fa fa-circle"></i> 即将过期</span>'
                                } else if (row.distance_days <= 0) {
                                    return '<span class="text-red"><i class="fa fa-circle"></i> 已过期</span>'
                                } else {
                                    return '<span class="text-green"><i class="fa fa-circle"></i> 正常</span>'
                                }
                            }
                        },
                        {
                            field: 'customer.full_name', title: __('客户名称')
                        },
                        {field: 'customer.idnumber', title: __('身份证号码')},
                        {field: 'customer.corporate_name', title: __('公司名称')},
                        {
                            field: 'service_name', title: __('服务名称'), operate: false,
                        },
                        {field: 'standard_price', title: __('标准价格'), operate: false},
                        {field: 'deal_price', title: __('成交价格'), operate: false},
                        {field: 'collection_price', title: __('已收款金额'), operate: false},
                        {field: 'balance_price', title: __('尾款金额'), operate: false},
                        // {field: 'account_commission', title: __('Account_commission'), operate: 'BETWEEN'},
                        // {field: 'sales_commission', title: __('Sales_commission'), operate: 'BETWEEN'},
                        // {
                        //     field: 'pay_pic',
                        //     title: __('Pay_pic'),
                        //     operate: 'LIKE',
                        //     table: table,
                        //     class: 'autocontent',
                        //     formatter: Table.api.formatter.content
                        // },
                        {
                            field: 'month',
                            title: __('服务期限'),
                            operate: false,
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: function (value, row, index) {
                                if (value == '-') {
                                    return value
                                }
                                return value ? value + '个月' : '一次性服务'
                            }
                        },
                        {
                            field: 'distance_days',
                            title: __('距离到期（月）'),
                            operate: false,
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: function (value, row, index) {
                                if (value == '-') {
                                    return value
                                }
                                return value + '个月'
                            }
                        },

                        {
                            field: 'start_time_text',
                            title: __('开始日期'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            // formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'end_time_text',
                            title: __('结束日期'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            // formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        disposable: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'customer/service/index' + location.search,
                    // add_url: 'customer/service/add',
                    edit_url: 'customer/service/edit',
                    // del_url: 'customer/service/del',
                    multi_url: 'customer/service/multi',
                    import_url: 'customer/service/import',
                    table: 'customer_service',
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                queryParams: (params) => {
                    var filter = JSON.parse(params.filter)
                    var op = JSON.parse(params.op)

                    filter.month = 0
                    op.month = '<='

                    filter['package.package_id'] = Config['package_ids']
                    op['package.package_id'] = 'in'

                    params.filter = JSON.stringify(filter)
                    params.op = JSON.stringify(op)

                    return params
                },
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id', title: __('Id'),
                            operate: false,
                        },
                        {field: 'package.admin_info.nickname', title: __('销售')},
                        {field: 'package.kuaiji_names', title: __('会计')},
                        {field: 'package.waiqin_names', title: __('外勤')},

                        {field: 'customer.full_name', title: __('客户名称')},
                        {field: 'customer.idnumber', title: __('身份证号码')},
                        {field: 'customer.corporate_name', title: __('公司名称')},
                        {field: 'service_name', title: __('服务名称'), operate: false,},
                        {field: 'standard_price', title: __('标准价格'), operate: false},
                        {field: 'deal_price', title: __('成交价格'), operate: false},
                        {field: 'collection_price', title: __('已收款金额'), operate: false},
                        {field: 'balance_price', title: __('尾款金额'), operate: false},
                        {
                            field: 'month',
                            title: __('服务期限'),
                            operate: false,
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: function (value, row, index) {
                                if (value == '-') {
                                    return value
                                }
                                return value ? value + '个月' : '一次性服务'
                            }
                        },
                        {
                            field: 'create_time',
                            title: __('添加日期'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        datasummary: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'customer/service/index' + location.search,
                    // add_url: 'customer/service/add',
                    edit_url: 'customer/service/edit',
                    // del_url: '
                    // customer/service/del',
                    multi_url: 'customer/service/multi',
                    import_url: 'customer/service/import',
                    table: 'customer_service',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                queryParams: (params) => {
                    var filter = JSON.parse(params.filter)
                    var op = JSON.parse(params.op)
                    filter['package.package_id'] = Config['package_ids']
                    op['package.package_id'] = 'in'
                    params.filter = JSON.stringify(filter)
                    params.op = JSON.stringify(op)
                    return params
                },
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id', title: __('Id'),
                            operate: false,
                        },
                        {field: 'package.admin_info.nickname', title: __('销售')},
                        {field: 'package.kuaiji_names', title: __('会计')},
                        {field: 'package.waiqin_names', title: __('外勤')},
                        {
                            field: 'status',
                            title: __('到期状态'),
                            operate: false,
                            // formatter: Table.api.formatter.status,
                            formatter: function (value, row, index) {

                                if (row.month == 0) {
                                    return '-'
                                }

                                if (row.distance_days == '-') {
                                    return row.distance_days
                                } else if (row.distance_days <= 2) {
                                    return '<span class="text-orange"><i class="fa fa-circle"></i> 即将过期</span>'
                                } else if (row.distance_days <= 0) {
                                    return '<span class="text-red"><i class="fa fa-circle"></i> 已过期</span>'
                                } else {
                                    return '<span class="text-green"><i class="fa fa-circle"></i> 正常</span>'
                                }
                            }
                        },
                        {
                            field: 'customer.full_name', title: __('客户名称')
                        },
                        {field: 'customer.idnumber', title: __('身份证号码')},
                        {field: 'customer.corporate_name', title: __('公司名称')},
                        {
                            field: 'service_name', title: __('服务名称'), operate: false,
                        },
                        {field: 'standard_price', title: __('标准价格'), operate: false},
                        {field: 'deal_price', title: __('成交价格'), operate: false},
                        {field: 'collection_price', title: __('已收款金额'), operate: false},
                        {field: 'balance_price', title: __('尾款金额'), operate: false},
                        // {field: 'account_commission', title: __('Account_commission'), operate: 'BETWEEN'},
                        // {field: 'sales_commission', title: __('Sales_commission'), operate: 'BETWEEN'},
                        // {
                        //     field: 'pay_pic',
                        //     title: __('Pay_pic'),
                        //     operate: 'LIKE',
                        //     table: table,
                        //     class: 'autocontent',
                        //     formatter: Table.api.formatter.content
                        // },
                        {
                            field: 'month',
                            title: __('服务期限'),
                            operate: false,
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: function (value, row, index) {
                                if (value == '-') {
                                    return value
                                }
                                return value ? value + '个月' : '一次性服务'
                            }
                        },
                        {
                            field: 'distance_days',
                            title: __('距离到期（月）'),
                            operate: false,
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: function (value, row, index) {
                                if (row.month == 0) {
                                    return '-'
                                }

                                if (value == '-') {
                                    return value
                                }
                                return value + '个月'
                            }
                        },

                        {
                            field: 'start_time_text',
                            title: __('开始日期'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: function (value, row, index) {
                                if (row.month == 0) {
                                    return '-'
                                }
                                return value
                            }
                        },
                        {
                            field: 'end_time_text',
                            title: __('结束日期'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: function (value, row, index) {
                                if (row.month == 0) {
                                    return '-'
                                }
                                return value
                            }
                            // formatter: Table.api.formatter.datetime
                        },

                        {
                            field: 'create_time',
                            title: __('录入时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
